/*Copyright 2014 Rajesh Putta http://www.codesnippets4all.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* 
*/

package com.codesnippets4all.jthunder.extension.plugins.input.csv;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/*
 * @author Rajesh Putta
 */
public class CsvData {
	
	private int totalRecords;
	
	private List<String> headers=new ArrayList<String>();
	
	private Map<Integer, List<String>> records=new LinkedHashMap<Integer, List<String>>();
	
	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
	}
	
	public int getTotalRecords() {
		return totalRecords;
	}
	
	public void setHeaders(String[] headers)
	{
		this.headers.addAll(Arrays.asList(headers));
	}
	
	public List<String> getHeaders()
	{
		return this.headers;
	}
	
	public void addRecord(Integer rowNumber, String[] record)
	{
		this.records.put(rowNumber, Arrays.asList(record));
	}
	
	public Map<Integer, List<String>> getRecords() {
		return this.records;
	}
	
	public String getCellData(Integer rowNumber, String header)
	{
		int column=this.headers.indexOf(header);
		
		if(column==-1)
			return null;
		
		List<String> record=this.records.get(rowNumber);
		
		if(record==null || record.isEmpty())
		{
			return null;
		}
		
		if(column>record.size()-1)
			return null;
		
		return record.get(column);
	}

	public String getCellData(Integer rowNumber, Integer columnNumber)
	{
		List<String> record=this.records.get(rowNumber);
		
		if(record==null || record.isEmpty())
		{
			return null;
		}
		
		if(columnNumber>record.size()-1)
			return null;
		
		return record.get(columnNumber);
	}
	
	public List<String> getRecord(Integer rowNumber)
	{
		return this.records.get(rowNumber);
	}
	
	public Map<String, String> getRecordMap(Integer rowNumber)
	{
		Map<String, String> recordMap=new HashMap<String, String>();
		
		List<String> recordData=this.records.get(rowNumber);
		
		int length=this.headers.size();
		
		for(int i=0;i<length;i++)
		{
			recordMap.put(this.headers.get(i), recordData.get(i));
		}
		
		return recordMap;
	}
	
}
